package com.zh.work.controller.platform.branch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.dao.platform.branch.BranchUserDaoMapper;
import com.zh.work.entity.branch.BranchUserEntity;
import com.zh.work.service.platform.branch.BranchUserService;
import com.zh.common.entity.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 机构用户管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/27
 * @Version: 1.0.1
 */
@Api(value = "BranchUserController", tags = "机构用户管理控制器")
@RestController
@RequestMapping("/branchUser")
public class BranchUserController {

    @Autowired
    private BranchUserService service;

    @Autowired
    private BranchUserDaoMapper mapper;

    @ApiOperation(value = "根据id查询详细信息", tags = {"getBranchUserById"}, notes = "根据id查询详细信息")
    @GetMapping("/getBranchUserById")
    public JSONResult<BranchUserEntity> getBranchUserById(@ApiParam(value = "机构操作员实体类", name = "branchUserEntity") BranchUserEntity branchUserEntity) {
        JSONResult<BranchUserEntity> jsonResult = new JSONResult<>();
        try {
            BranchUserEntity result = branchUserEntity.selectById(branchUserEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModBranchUserById"}, notes = "通过id修改信息,请使用application/json格式提交哦!")
    @PutMapping("/putModBranchUserById")
    @ResponseBody
    public JSONResult<BranchUserEntity> putModBranchUserById(@ApiParam(value = "机构操作员实体类", name = "branchUserEntity")
                                                                 @RequestBody BranchUserEntity branchUserEntity) {
        JSONResult<BranchUserEntity> jsonResult = new JSONResult<>();
        try {
            if (branchUserEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "重置机构管理员密码,请谨慎操作!", tags = {"putModBranchUserPassWord"}, notes = "重置机构管理员密码,请谨慎操作!")
    @PutMapping("/putModBranchUserPassWord")
    @ResponseBody
    public JSONResult<Object> putModBranchUserPassWord(@ApiParam(value = "机构管理员实体类", name = "branchUserEntity")@RequestBody
                                                                   BranchUserEntity branchUserEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        int i = this.service.updateBranchUserPassWord(branchUserEntity);
        if (i != 500 && i != 204) {
            jsonResult.setMessage("操作成功!");
            jsonResult.setData(i);
            return jsonResult;
        } else {
            jsonResult.setCode(i);
            jsonResult.setSuccess(false);
            jsonResult.setMessage("操作失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteBranchUserById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteBranchUserById")
    public JSONResult<Object> deleteBranchUserById(@ApiParam(value = "机构管理员实体类", name = "branchUserEntity") BranchUserEntity branchUserEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchUserEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "锁定机构管理员,请谨慎操作!", tags = {"putModBranchUserAccount"}, notes = "锁定机构管理员,请谨慎操作!")
    @PutMapping("/putModBranchUserAccount")
    @ResponseBody
    public JSONResult<Object> putModBranchUserAccount(@ApiParam(value = "机构管理员实体类", name = "branchUserEntity")
                                                                  @RequestBody BranchUserEntity branchUserEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if ( this.service.updateById(branchUserEntity)) {
                jsonResult.setMessage("操作成功!");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("操作失败!");
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesBranchUsers"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesBranchUsers")
    public JSONResult<List<BranchUserEntity>> getPagesBranchUsers(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "显示数量页数", name = "pageNo") Integer pageSize,
            @ApiParam(value = "机构管理员实体类", name = "branchUserEntity") BranchUserEntity branchUserEntity) {
        JSONResult<List<BranchUserEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<BranchUserEntity> page = this.mapper.selectPage(
                    new Page<>(pageNo, pageSize),
                    new QueryWrapper<BranchUserEntity>()
                            .eq(Objects.nonNull(branchUserEntity.getUserRoler()), "user_roler", branchUserEntity.getId())
                            .eq(Objects.nonNull(branchUserEntity.getStatus()), "status", branchUserEntity.getStatus())
                            .likeLeft(StringUtils.isNotEmpty(branchUserEntity.getLoginName()), "login_name", branchUserEntity.getLoginName())
                            .likeLeft(StringUtils.isNotEmpty(branchUserEntity.getMobile()), "mobile", branchUserEntity.getMobile())
                            .likeLeft(StringUtils.isNotEmpty(branchUserEntity.getTrueName()), "true_name", branchUserEntity.getTrueName())
            );
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(page.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }




}
